//
//  Login.m
//  Maze
//
//  Created by Lin : on 9/13/11.
//  Copyright 2011 tina. All rights reserved.
//

#import "Login.h"
#import "LoginBackgroundLayer.h"
#import "OptionLayer.h"
#define BACKGROUND -2
#define LOGINBUTTON 0
#define OPTIONBUTTON 1

@implementation Login

@synthesize tempLabel;
@synthesize nameField;
@synthesize passField;

+(CCScene*)scene
{
	CCScene *newScene = [CCScene node];
	
	Login* loginPage = [Login node];
	
	[newScene addChild:loginPage z:0 tag:1];
    
    LoginBackgroundLayer *background = [LoginBackgroundLayer node];
    
    [newScene addChild:background z:BACKGROUND];
    
	
	return newScene;
}

-(id)init
{
	if ((self = [super init])) {
		[self setupMenu];
        
        //code here is for debug use. delete it when real login page is on
        /*tempLabel = [CCLabelTTF labelWithString:@"This is the Login Page. Press anybutton go to RoomList screen." 
                                     dimensions:CGSizeMake(400, 150) 
                                      alignment:UITextAlignmentCenter
                                       fontName:@"Verdana-Bold"
                                       fontSize:25];
         */
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        /*tempLabel.position = ccp(winSize.width - tempLabel.contentSize.width, winSize.height - tempLabel.contentSize.height);
        [self addChild:tempLabel];
        */
        [self setupMenu];
        
        float x = winSize.width;
        float rate = x/1024;
        
        //test field
        EAGLView *_view = [[CCDirector sharedDirector]openGLView];
        
		nameField = [[UITextField alloc]initWithFrame:CGRectMake(winSize.width*0.41*rate, 180.0*rate, 250.0*rate, 150.0*rate)];
        nameField.transform = CGAffineTransformRotate(nameField.transform, (M_PI *2));
        nameField.adjustsFontSizeToFitWidth = YES;
        nameField.placeholder = @"<Enter Your Name>";
		nameField.backgroundColor = [UIColor clearColor];
		nameField.textAlignment = UITextAlignmentCenter;
        nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        nameField.keyboardType = UIKeyboardTypeDefault;
        nameField.returnKeyType = UIReturnKeyDone;
		nameField.clearButtonMode = UITextFieldViewModeNever;
        nameField.borderStyle = UITextBorderStyleNone;
        nameField.background = [UIImage imageNamed:@"TextField.png"];
        [_view addSubview: nameField];
		
        
		passField = [[UITextField alloc]initWithFrame:CGRectMake(winSize.width*0.41*rate, 353.0*rate, 250.0*rate, 150.0*rate)];
        passField.transform = CGAffineTransformRotate(passField.transform, (M_PI *2));
        passField.adjustsFontSizeToFitWidth = YES;
        passField.placeholder = @"<Enter Your Password>";
		passField.backgroundColor = [UIColor clearColor];
		passField.textAlignment = UITextAlignmentCenter;
        passField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        passField.keyboardType = UIKeyboardTypeDefault;
        passField.returnKeyType = UIReturnKeyDone;
		passField.clearButtonMode = UITextFieldViewModeNever;
        passField.borderStyle = UITextBorderStyleNone;
        [passField setSecureTextEntry:YES];
        passField.background = [UIImage imageNamed:@"TextField.png"];
        [_view addSubview: passField];
        
        
        }
	
	return self;
}

-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:NO];
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint touchStop = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
    id action = [CCMoveTo actionWithDuration:(0.5) position:touchStop];
	[self runAction:action];
}




-(BOOL)textFieldShouldReturn:(UITextField *)textfield{
    if (textfield == nameField) {
        
        [nameField resignFirstResponder];
        return YES;
        
    }
	if (textfield == passField) {
        
        [passField resignFirstResponder];
        return YES;
    }
	return NO;
}

-(void)setupMenu
{
	// Create some menu items
    CGSize winSize = [[CCDirector sharedDirector] winSize];
	CCMenuItemImage * menuItem1 = [CCMenuItemImage itemFromNormalImage:@"LoginButton.png"
														 selectedImage: @"LoginPressed.png"
																target:self
															  selector:@selector(transit:)];
	
    menuItem1.position = ccp(winSize.width * 0.39, winSize.height*0.28);
    menuItem1.tag = LOGINBUTTON;
    
	CCMenuItemImage * menuItem2 = [CCMenuItemImage itemFromNormalImage:@"OptionButton.png"
														 selectedImage: @"OptionPressed.png"
																target:self
															  selector:@selector(transit:)];
		
	menuItem2.position = ccp(winSize.width * 0.7, winSize.height*0.28);
    menuItem2.tag = OPTIONBUTTON;
	// Create a menu and add your menu items to it
	CCMenu * myMenu = [CCMenu menuWithItems:menuItem1, menuItem2, nil];
	
	// Arrange the menu items vertically
	[myMenu alignItemsHorizontally];
	
    
    myMenu.position = ccp(winSize.width*0.5, winSize.height*0.28);
    
	// add the menu to your scene
	[self addChild:myMenu];
   // [self addChild:menuItem2];
}

-(void)transit:(CCMenuItem*)sender
{
    if(sender.tag == LOGINBUTTON){
	//connect to server
    if ([PackageParser sharedPackageParser].state == PlayerStateNone) {
        NSString *name = nameField.text;
        NSString *password = passField.text;
        //[PackageParser sharedPackageParser].player = [Player playerWithPlayerName:@"tina" Pwd:@"test"];
        [PackageParser sharedPackageParser].player.name = name;
        [PackageParser sharedPackageParser].player.pwd = password;
        [PackageParser sharedPackageParser].state = PlayerStateLoggingIn;
    }else
    {
        NSLog(@"this account has already been logged in");
    }
    }
    else if(sender.tag == OPTIONBUTTON){
       
    }
	//if succeed, switch to roomList page
	//[[CCDirector sharedDirector] replaceScene:[RoomList sceneWithRoomList:[NSMutableArray array]]];
	
	//else show connection error page
    
    
}

-(void)changeSceneTo:(CCScene*)scene
{
    [[CCDirector sharedDirector] replaceScene:scene];
}

-(void)dealloc
{
    //[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
    [nameField removeFromSuperview];
    [passField removeFromSuperview];
    [nameField release];
    nameField = nil;
    [passField release];
    passField = nil;
    [tempLable release];
    tempLabel = nil;
	[super dealloc];
}
@end
